'''
case Type :数据库系统
case Name ：主机业务压力，备机kill掉重启，反复多次，主机业务无影响，备机重启后数据同步正确
Create At: 2020-7-7
owner : @peilinqian
description:1、主机进行循环连接插入数据2、主机插入过程中备机进行3次kill重启操作3、主机插入操作正常，备机数据同步正确
history：
created： 2020/07/07  created by @peilinqian
'''

import os
import time
import threading
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case021.py start------------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.standby1_root_node = Node(node='Standby1Root')
        self.standby1_user_node = Node(node='Standby1DbUser')
        self.standby2_user_node = Node(node='Standby2DbUser')
        self.com = Common()

    def test_main(self):
        # 执行建表语句，准备测试数据
        createcmd = 'drop table if exists test_020;create table test_020(id int,name varchar(20));'
        self.log.info(createcmd)
        msg = self.primary_sh.executDbSql(createcmd)
        self.log.info(msg)

        # 执行insert sql语句
        insertcount = 1000
        insertcmd = 'insert into test_020 values(1,\'testaaaaaaaaaa\');'
        insertthread = threading.Thread(target=self.primary_sh.cycle_exec_sql, args=(insertcmd, insertcount,))
        insertthread.setDaemon(True)
        insertthread.start()

        time.sleep(3)

        # kill备机并重启3次
        msg = self.com.kill_pid(self.standby1_root_node, 9)
        self.assertEqual(msg, '')

        time.sleep(3)

        is_started = self.primary_sh.startDbCluster()
        self.assertTrue(is_started)

        msg = self.com.kill_pid(self.standby1_root_node, 9)
        self.assertEqual(msg, '')

        time.sleep(3)

        is_started = self.primary_sh.startDbCluster()
        self.assertTrue(is_started)

        msg = self.com.kill_pid(self.standby1_root_node, 9)
        self.assertEqual(msg, '')

        time.sleep(3)

        is_started = self.primary_sh.startDbCluster()
        self.assertTrue(is_started)

        insertthread.join(600)

        # 查询主备机状态正常
        status = self.primary_sh.getDbClusterStatus('status')
        self.assertTrue(status)

        # 查询主机插入数据量正确
        select_cmd = 'select count(*) from test_020;'
        self.log.info(select_cmd)

        # 校验备机是否完成数据同步
        self.log.info('校验备机是否完成数据同步')
        flag = self.standby1_sh.check_data_consistency()
        self.assertTrue(flag)

        # 判断插入完成主备数据一致
        nodes_tuple = (self.primary_user_node, self.standby1_user_node, self.standby2_user_node)
        flag = self.com.check_data_sample_by_all(select_cmd, *nodes_tuple)
        self.assertTrue(flag)

    def tearDown(self):
        # 清除测试数据
        drop_sql = 'drop table test_020;'
        self.log.info(drop_sql)
        msg = self.primary_sh.executDbSql(drop_sql)
        self.log.info(msg)
        self.assertTrue('DROP TABLE' in msg)
